package com.inspur.gs.sbssdl.database.rysj.rysj.front.querydevicecount.voactions;

import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.caf.db.dbaccess.IConverToListFunc;
import com.inspur.edp.caf.db.dbaccess.IDbParameter;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class GetDeviceCountVOAction extends AbstractFSAction<Object> {

    private String param;

    public GetDeviceCountVOAction(String param) {

        this.param = param;
    }

    private IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

    @Override
    public void execute() {
        List<IDbParameter> params = new ArrayList<>();
        String sql = "select COUNT(tz.sbbh) from SBSS_SBTZ_SBTZ tz ";
        bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);
        List<IEntityData> entityData = bqlExecuter.executeSelectStatement(sql, new ArrayList<>(),
                new Iconver(), params.toArray(new IDbParameter[]{}));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalCount", entityData.size());
        setResult(resultMap);
    }

    public class Iconver implements IConverToListFunc<IEntityData> {

        @Override
        public List<IEntityData> convert(List<DynamicResultRow> list) {
            ArrayList<IEntityData> entityData = new ArrayList<>();
            if (!list.isEmpty()) {
                for (DynamicResultRow resultRow : list) {
                    IEntityData voEntityData = getContext().createData();
                    entityData.add(voEntityData);
                }
            }
            return entityData;
        }
    }

}
